"""
Some helpers to handle languages etc.
"""
__revision__ = "$Id: i18n.py 34 2007-02-12 12:35:22Z jens.persson $"

from turbogears.util import request_available
from turbogears import config
from cherrypy import request
from worde.templatehelpers import get_config
from turbogears.i18n.utils import _get_locale_from_accept_header

def get_locale():
    """
    Return the locale, according to the settings for global_locale and admin_locale
    based on turbogears.i18n.utils._get_locale()
    """

    if not request_available():
        return config.get("i18n.default_locale", "en")

    if request.path.startswith("/admin"):
        localesetting = get_config("admin locale", "Auto")
    else:
        localesetting = get_config("global locale", "en")

    if localesetting != "Auto":
        return localesetting
    else:
        browser_accept_lang = _get_locale_from_accept_header()
        return browser_accept_lang or config.get("i18n.default_locale", "en")

__cfg__ = {}
__cfg__["i18n.get_locale"] = get_locale
config.update(__cfg__)

if __name__ == '__main__':
    pass